﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class TravelNotesController : ApiController
    {
        TravelNotesBO _TravelNotesBO = new TravelNotesBO();
        #region 游记信息 全部记录
        [HttpGet]
        public IHttpActionResult GetNotesList()
        {
            try
            {
                var result = new TravelNotesBO().GetNotesList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游记信息 单条记录
        [HttpPost]
        public IHttpActionResult GetNotesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int NoteID = model.NoteID ?? 0;
            if (NoteID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _TravelNotesBO.GetNotesInfo(NoteID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游记信息 添加
        [HttpPost]
        public IHttpActionResult InsertNotesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string TouristID = model.TouristID ?? "";
            string Title = model.Title ?? "";
            string Content = model.Content ?? "";
            string PublishDate = model.PublishDate ?? "";
            string Views = model.Views ?? "";
            string Likes = model.Likes ?? "";
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(Title))
            {
                return Json(new Result(ResultCode.Fail, "标题不能为空"));
            }
            if (string.IsNullOrEmpty(Content))
            {
                return Json(new Result(ResultCode.Fail, "内容不能为空"));
            }
            if (string.IsNullOrEmpty(PublishDate))
            {
                return Json(new Result(ResultCode.Fail, "发表日期不能为空"));
            }
            try
            {
                var result = _TravelNotesBO.InsertNotesInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游记信息 修改
        [HttpPost]
        public IHttpActionResult UpdateNotesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int NoteID = model.NoteID ?? 0;
            string TouristID = model.TouristID ?? "";
            string Title = model.Title ?? "";
            string Content = model.Content ?? "";
            string PublishDate = model.PublishDate ?? "";
            string Views = model.Views ?? "";
            string Likes = model.Likes ?? "";
            if (NoteID == 0)
            {
                return Json(new Result(ResultCode.Fail, "游记ID不能为空"));
            }
            if (string.IsNullOrEmpty(TouristID))
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(Title))
            {
                return Json(new Result(ResultCode.Fail, "标题不能为空"));
            }
            if (string.IsNullOrEmpty(Content))
            {
                return Json(new Result(ResultCode.Fail, "内容不能为空"));
            }
            try
            {
                var result = _TravelNotesBO.UpdateNotesInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) :
                Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游记信息 删除
        [HttpPost]
        public IHttpActionResult DeleteNotesInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int NoteID = model.NoteID ?? 0;
            if (NoteID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _TravelNotesBO.DeleteNotesInfo(NoteID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 用户信息 分页列表
        [HttpPost]
        public IHttpActionResult PageNotesList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail,
                "参数错误"));
                var wc = _PageNotesListWhereC(model.condidate);
                var result = _TravelNotesBO.PageNotesList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _PageNotesListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.TouristID);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" TouristID like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.Title);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" Title like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.Content);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" Content like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.PublishDate);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" PublishDate like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.Views);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" Views like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.Likes);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" Likes like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}
